package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.system.domain.bo.SysNoticeBo;
import com.ruoyi.system.domain.vo.SysNoticeVo;
import com.ruoyi.system.domain.SysNotice;
import com.ruoyi.system.mapper.SysNoticeMapper;
import com.ruoyi.system.service.ISysNoticeService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 通知公告Service业务层处理
 *
 * @author ruoyi
 * @date 2024-10-23
 */
@RequiredArgsConstructor
@Service
public class SysNoticeServiceImpl extends ServiceImpl<SysNoticeMapper,SysNotice> implements ISysNoticeService  {


    /**
     * 查询通知公告
     */
    @Override
    public SysNoticeVo queryById(Long noticeId){
        return baseMapper.selectVoById(noticeId);
    }

    /**
     * 查询通知公告列表
     */
    @Override
    public TableDataInfo<SysNoticeVo> queryPageList(SysNoticeBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SysNotice> lqw = buildQueryWrapper(bo);
        Page<SysNoticeVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询通知公告列表
     */
    @Override
    public List<SysNoticeVo> queryList(SysNoticeBo bo) {
        LambdaQueryWrapper<SysNotice> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SysNotice> buildQueryWrapper(SysNoticeBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SysNotice> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getNoticeTitle()), SysNotice::getNoticeTitle, bo.getNoticeTitle());
        lqw.eq(StringUtils.isNotBlank(bo.getNoticeType()), SysNotice::getNoticeType, bo.getNoticeType());
        lqw.eq(StringUtils.isNotBlank(bo.getNoticeContent()), SysNotice::getNoticeContent, bo.getNoticeContent());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), SysNotice::getStatus, bo.getStatus());
        lqw.eq(bo.getSender() != null, SysNotice::getSender, bo.getSender());
        lqw.eq(StringUtils.isNotBlank(bo.getPriority()), SysNotice::getPriority, bo.getPriority());
        lqw.eq(StringUtils.isNotBlank(bo.getMsgType()), SysNotice::getMsgType, bo.getMsgType());
        lqw.eq(StringUtils.isNotBlank(bo.getSendStatus()), SysNotice::getSendStatus, bo.getSendStatus());
        lqw.eq(bo.getSendTime() != null, SysNotice::getSendTime, bo.getSendTime());
        lqw.eq(bo.getCancelTime() != null, SysNotice::getCancelTime, bo.getCancelTime());
        return lqw;
    }

    /**
     * 新增通知公告
     */
    @Override
    public Boolean insertByBo(SysNoticeBo bo) {
        SysNotice add = BeanUtil.toBean(bo, SysNotice.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setNoticeId(add.getNoticeId());
        }
        return flag;
    }

    /**
     * 修改通知公告
     */
    @Override
    public Boolean updateByBo(SysNoticeBo bo) {
        SysNotice update = BeanUtil.toBean(bo, SysNotice.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(SysNotice entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除通知公告
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public Page<SysNotice> querySysNoticePageByUserId(Page<SysNotice> page, Long userId, String msgCategory) {
        if (page.getSize() == -1) {
            return page.setRecords(baseMapper.querySysNoticeListByUserId(null, userId.toString(), msgCategory));
        } else {
            return page.setRecords(baseMapper.querySysNoticeListByUserId(page, userId.toString(), msgCategory));
        }
    }
}
